package com.jike.noobmoney.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.ShenHeDetailEntity;
import com.jike.noobmoney.helper.ImageLoader;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.widget.BuHeGeDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxBusRoute;
import com.jike.noobmoney.rxbus.RxEvent;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.Constant;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.SignUtil;
import com.jike.noobmoney.util.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 审核详情页面
 */
public class ShenHeDetailActivity extends BaseActivity implements IView {

    public static final String o_id = "o_id";
    public static final String o_status = "o_status";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.ll_image)
    LinearLayout llImage;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_yes)
    TextView tvYes;
    @BindView(R.id.ll_shenhe)
    LinearLayout llShenhe;
    private String orderId,statusNum;
    private TaskPresenter taskPresenter;

    @BindView(R.id.tv_task_name)
    TextView tv_task_name;

    @BindView(R.id.tv_jieduantime)
    TextView tv_jieduantime;
    @BindView(R.id.tv_tijiaotime)
    TextView tv_tijiaotime;

    @BindView(R.id.tv_user_id)
    TextView tv_user_id;

    @BindView(R.id.ll_isinfo)
    LinearLayout ll_isinfo;

    @BindView(R.id.tv_ts)
    TextView tv_ts;


    public static void startAct(Context context, String o_id,String status) {
        Intent intent = new Intent(context, ShenHeDetailActivity.class);
        intent.putExtra(ShenHeDetailActivity.o_id, o_id);
        intent.putExtra(ShenHeDetailActivity.o_status, status);
        context.startActivity(intent);
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_shen_he_detail;
    }

    @Override
    protected void initData() {
        if (getIntent() != null) {
            orderId = getIntent().getStringExtra(ShenHeDetailActivity.o_id);
            statusNum = getIntent().getStringExtra(ShenHeDetailActivity.o_status);
        }
        taskPresenter = new TaskPresenter(this);
        getData();
    }
    /**
     * 如果要全屏,重写这个方法返回false
     */
    public Boolean isfits() {
        return true;
    }

    /**
     * 如果要白色主题,重写这个方法返回false
     */
    public boolean ismain() {
        return true;
    }
    /**
     * 获取任务详情
     */
    private void getData() {
        tvTitle.setText("审核详情");
        showProgress();
        taskPresenter.getShenHeDetail(orderId, ConstantValue.RequestKey.shenhe_detail);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.shenhe_detail.equals(actionType)) {
            ShenHeDetailEntity shenHeDetailEntity = (ShenHeDetailEntity) data;
            if (shenHeDetailEntity != null) {
                List<ShenHeDetailEntity.OrderlpicistBean> orderlpicist = shenHeDetailEntity.getOrderlpicist();
                List<ShenHeDetailEntity.OrderinfoBean> orderinfo = shenHeDetailEntity.getOrderinfo();

                if (orderinfo != null && orderinfo.size() > 0) {
                    ShenHeDetailEntity.OrderinfoBean orderinfoBean = orderinfo.get(0);

                    //编号
                    tvOrderNumber.setText(orderinfoBean.getT_id()+"");

                    //金额
                    tvTotalMoney.setText("" + orderinfoBean.getMoney());
                    //任务名称
                    tv_task_name.setText("任务名称："+orderinfoBean.getTaskname());
                    //截单时间
                    tv_jieduantime.setText("接单时间："+orderinfoBean.getCreatetime());
                    //提交时间；
                    tv_tijiaotime.setText("提交时间："+orderinfoBean.getSubmittime());
                    //用户
                    tvUser.setText("用户:" + ComUtils.jiequPhone(orderinfoBean.getMobile()));
                    //用户id
                    tv_user_id.setText("用户id:"+orderinfoBean.getU_id()+"");
                    tvTime.setText("历史任务做单通过率:"+ ComUtils.getPercent(orderinfoBean.finishpercent));

                    if(orderinfoBean.isinfo.equals("0")){
                        ll_isinfo.setVisibility(View.GONE);
                    }else
                    {
                        ll_isinfo.setVisibility(View.VISIBLE);
                        tvText.setText(orderinfoBean.getInfo());
                    }

                    if(statusNum.equals("5")){
                        tvNo.setVisibility(View.GONE);
                        tvYes.setVisibility(View.GONE);
                        tv_ts.setVisibility(View.VISIBLE);
                    }else {

                        int status = orderinfoBean.getStatus();

                        if (status == 3) {
                            tvNo.setVisibility(View.VISIBLE);
                            tvYes.setVisibility(View.VISIBLE);
                            tv_ts.setVisibility(View.VISIBLE);
                        } else if (status == 4) {
                            tvNo.setVisibility(View.GONE);
                            tvYes.setVisibility(View.GONE);
                            tv_ts.setVisibility(View.GONE);
                        } else if (status == 2) {
                            if (orderinfoBean.istaskappeal.equals("1")) {
                                tvNo.setVisibility(View.GONE);
                                tvYes.setVisibility(View.GONE);
                                tv_ts.setVisibility(View.GONE);
                            } else {
                                tvNo.setVisibility(View.GONE);
                                tvYes.setVisibility(View.VISIBLE);
                                tv_ts.setVisibility(View.GONE);
                            }

                        } else if (status == 5) {
                            tvNo.setVisibility(View.VISIBLE);
                            tvYes.setVisibility(View.VISIBLE);
                            tv_ts.setVisibility(View.VISIBLE);
                        }
                    }


                    //提交时间
//                    int status = orderinfoBean.getStatus();
//                    if (status == 3) {
//                        tvTime.setVisibility(View.VISIBLE);
//                        tvTime.setText("提交时间:" + orderinfoBean.getSubmittime());
//                        llShenhe.setVisibility(View.VISIBLE);
//                    } else if (status == 4) {
//                        tvTime.setVisibility(View.VISIBLE);
//                        tvTime.setText("完成时间:" + orderinfoBean.getFinishtime());
//                        llShenhe.setVisibility(View.GONE);
//                    }

                }

                if (orderlpicist != null && orderlpicist.size() > 0) {
                    //循环图片
                    for (int i = 0; i < orderlpicist.size(); i++) {
                        ShenHeDetailEntity.OrderlpicistBean orderlpicistBean = orderlpicist.get(i);
                        String picture = orderlpicistBean.getPicture();
                        String url = ConstantValue.IMG_BASEURL_QR_CODE + picture;
                        addImg(url);
                    }
                }


            }
        }

        if (ConstantValue.RequestKey.shenhe_yes.equals(actionType)) {
            ToastUtils.showShortToastSafe("审核通过");
            RxBus.getInstance().post(new RxEvent(RxBusRoute.SHEHE));
            finish();
        }

        if (ConstantValue.RequestKey.shenhe_no.equals(actionType)) {
            ToastUtils.showShortToastSafe("提交成功");
            RxBus.getInstance().post(new RxEvent(RxBusRoute.SHEHE));
            finish();
        }
        if (ConstantValue.RequestKey.tousu.equals(actionType)) {
            ToastUtils.showShortToastSafe("投诉提交成功");
            RxBus.getInstance().post(new RxEvent(RxBusRoute.SHEHE));
            finish();
        }


    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);

    }

    @OnClick({R.id.iv_back, R.id.tv_no, R.id.tv_yes,R.id.tv_ts})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_no:
                if (AppUtils.isFastClick()) {
                    ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
                    return;
                }

                BuHeGeDialog jiaPiaoDialog = BuHeGeDialog.getInstance();
                jiaPiaoDialog.show(getSupportFragmentManager(), "buhege");
                jiaPiaoDialog.setClickCallback(number -> {
                    //确认
                    jiaPiaoDialog.dismiss();
                    showProgress();
                    taskPresenter.shenHeNo(orderId, number, ConstantValue.RequestKey.shenhe_no);
                });


                break;
            case R.id.tv_ts:
                if (AppUtils.isFastClick()) {
                    ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
                    return;
                }

                BuHeGeDialog jiaPiaoDialog1 = BuHeGeDialog.getInstance();
                jiaPiaoDialog1.show(getSupportFragmentManager(), "buhege1");
                jiaPiaoDialog1.setClickCallback(number -> {
                    //确认
                    jiaPiaoDialog1.dismiss();
                    showProgress();
                    taskPresenter.tousu(orderId, number, ConstantValue.RequestKey.tousu);
                });


                break;
            case R.id.tv_yes:
                if (AppUtils.isFastClick()) {
                    ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
                    return;
                }
                showProgress();
                String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

                TreeMap<String,String> paraMap = new TreeMap<String,String>();
                paraMap.put("o_id",orderId);
                paraMap.put("u_id",userid+"");
                String sign= SignUtil.generateSignature(paraMap, Constant.VALUE);
                taskPresenter.shenHeYes(orderId, userid,sign, ConstantValue.RequestKey.shenhe_yes);
                break;
        }
    }

    private void addImg(String url) {
        ImageView newImg = new ImageView(this);
        //设置想要的图片，相当于android:src="@drawable/image"
        //newImg.setImageResource(R.mipmap.ic_launcher);
        ImageLoader.displayImage(this, url, newImg);

        //设置子控件在父容器中的位置布局，wrap_content,match_parent
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        // 也可以自己想要的宽度，参数（int width, int height）均表示px
        // 如dp单位，首先获取屏幕的分辨率在求出密度，根据屏幕ppi=160时，1px=1dp
        //则公式为 dp * ppi / 160 = px ——> dp * dendity = px
        //如设置为48dp：1、获取屏幕的分辨率 2、求出density 3、设置
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        float density = displayMetrics.density;
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                (int) (48 * density),
                (int) (48 * density));
        //相当于android:layout_marginLeft="8dp"
        params.leftMargin = 20;
        params.rightMargin = 20;
        params.topMargin = 20;
        //addView(View child)，默认往已有的view后面添加，后插入，不设置布局params,默认wrap_content
        //llImage.addView(newImg);
        //addView(View child, LayoutParams params)，往已有的view后面添加，后插入,并设置布局
        llImage.addView(newImg, params);
    }
}
